//机构后台主页
exports.organization_master_index = function (req, res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    var platformNotificationCount = 0;

    var organizationHasViewCount = 0;

    var hasVerifiedMoneyData = 0;

    var hasNotPaid = 0;

    var hasRefunded = 0;

    var hasCommented = 0;

    var hasPaidFail = 0;
    var hasPaid = 0;
    var hasVerified = 0;


    //任务流创建
    async.waterfall([
            //1.未付款订单
            function (cb) {
                // //console.log(1111);
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_order + "/count/1/o_oid/" + restfulParams.organization_id + "/o_pay_status/0",
                        method: 'POST',
                        data: {
                            fields: ["o_pay_money"].join(",")
                        }
                    },
                    function (err, data) {

                        // console.log(1111, data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        var all_money = data.data;

                        var money = 0;

                        for (var i = 0; i < all_money.length; i++) {

                            money += parseFloat(all_money[i].o_pay_money);

                        }

                        hasNotPaid = parseFloat(money).toFixed(2);

                        cb();
                    }
                );

            },
            //1.退款款订单
            function (cb) {
                // //console.log(1111);
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_order + "/count/1/o_oid/" + restfulParams.organization_id + "/o_pay_status/6",
                        method: 'POST',
                        data: {
                            fields: ["o_pay_money"].join(",")
                        }
                    },
                    function (err, data) {
                        // console.log(2222, data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        var all_money = data.data;

                        var money = 0;

                        for (var i = 0; i < all_money.length; i++) {

                            money += parseFloat(all_money[i].o_pay_money);

                        }

                        hasRefunded = parseFloat(money).toFixed(2);

                        cb();
                    }
                );

            },
            //1.已评价订单
            function (cb) {
                // //console.log(1111);
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_order + "/count/1/o_oid/" + restfulParams.organization_id + "/o_pay_status/7",
                        method: 'POST',
                        data: {
                            fields: ["o_pay_money"].join(",")
                        }
                    },
                    function (err, data) {
                        // console.log(3333, data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        var all_money = data.data;

                        var money = 0;

                        for (var i = 0; i < all_money.length; i++) {

                            money += parseFloat(all_money[i].o_pay_money);

                        }

                        hasCommented = parseFloat(money).toFixed(2);

                        cb();
                    }
                );

            },
            //1.已支付订单
            function (cb) {
                // //console.log(1111);
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_order + "/count/1/o_oid/" + restfulParams.organization_id + "/o_pay_status/1",
                        method: 'POST',
                        data: {
                            fields: ["o_pay_money"].join(",")
                        }
                    },
                    function (err, data) {
                        // console.log(4444, data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        var all_money = data.data;

                        var money = 0;

                        for (var i = 0; i < all_money.length; i++) {

                            money += parseFloat(all_money[i].o_pay_money);

                        }

                        hasPaid = parseFloat(money).toFixed(2);

                        cb();
                    }
                );

            },
            //1.已验证订单
            function (cb) {
                // //console.log(1111);
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_order + "/count/1/o_oid/" + restfulParams.organization_id + "/o_pay_status/3",
                        method: 'POST',
                        data: {
                            fields: ["o_pay_money"].join(",")
                        }
                    },
                    function (err, data) {
                        // console.log(5555, data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        var all_money = data.data;

                        var money = 0;

                        for (var i = 0; i < all_money.length; i++) {

                            money += parseFloat(all_money[i].o_pay_money);

                        }

                        hasVerified = parseFloat(money).toFixed(2);

                        cb();
                    }
                );

            },
            //1.付款失败订单
            function (cb) {
                // //console.log(1111);
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_order + "/count/1/o_oid/" + restfulParams.organization_id + "/o_pay_status/2",
                        method: 'POST',
                        data: {
                            fields: ["o_pay_money"].join(",")
                        }
                    },
                    function (err, data) {
                        // console.log(6666, data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        var all_money = data.data;

                        var money = 0;

                        for (var i = 0; i < all_money.length; i++) {

                            money += parseFloat(all_money[i].o_pay_money);

                        }

                        hasPaidFail = parseFloat(money).toFixed(2);

                        cb();
                    }
                );

            },
            //1.订单总额、订单总数量
            function (cb) {
                // //console.log(1111);
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_order + "/count/1/o_oid/" + restfulParams.organization_id  + "/o_pay_status/3",
                        method: 'POST',
                        data: {
                            fields: ["o_pay_money"].join(",")
                        }
                    },
                    function (err, data) {
                        // console.log(7777, data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        var all_money = data.data;

                        var money = 0;

                        for (var i = 0; i < all_money.length; i++) {

                            money += parseFloat(all_money[i].o_pay_money);

                        }

                  
                        // resultData.allMoneyData = parseFloat(parseFloat(money) - parseFloat(hasNotPaid) - parseFloat(hasRefunded) - parseFloat(hasPaidFail)).toFixed(2);
                        resultData.allMoneyData = parseFloat(parseFloat(hasPaid) + parseFloat(hasCommented) + parseFloat(hasVerified)).toFixed(2);
                        resultData.orderCountData = data.count;

                        cb();
                    }
                );


                cb();

            },
            //2.未验证订单数量
            function (cb) {
                // //console.log(2222);
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrderFixConsume + "/count/1/cn_oid/" + restfulParams.organization_id + "/cn_status/0",
                        method: 'POST',
                        data: {
                            fields: ["cn_id"].join(",")
                        }
                    },
                    function (err, data) {
                        // console.log(8888, data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.unusedCodeCount = data.count;

                        cb();
                    }
                );

            },
            /*function (cb) {
                    // //console.log(2222);
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.search_verification_code + "/count/1/cn_oid/" + restfulParams.organization_id + "/cn_status/0",
                            method: 'POST',
                            data: {
                                fields: ["cn_id"].join(",")
                            }
                        },
                        function (err, data) {
                            //判断是否有错误信息
                            if (err) {
                                return cb(err, data);
                            }

                            resultData.unusedCodeCount = data.count;

                            cb();
                        }
                    );

                },*/
            //3.活动浏览量、活动人次、发起过的活动数量
            function (cb) {
                // //console.log(3333);
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganizationActivity + "/count/1/oa_oid/" + restfulParams.organization_id,
                        method: 'POST',
                        data: {
                            fields: ["oa_id", "oa_join_count", "oa_oid"].join(",")
                        }
                    },
                    function (err, data) {
                        // console.log(9999, data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        var activity = data.data;

                        var join_count = 0;//参与人次

                        for (var i = 0; i < activity.length; i++) {

                            join_count += parseInt(activity[i].oa_join_count);

                        }
                        // resultData.activityData = activity;

                        resultData.joinCount = join_count;

                        resultData.activityCount = data.count;

                        cb();
                    }
                );

            },
            //查询机构浏览量
            function (cb) {
                
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.searchOrganization + "/o_id/" + restfulParams.organization_id,
                            method: 'POST',
                            data: {
                                fields: ["o_id", "o_browse_times", "o_name", "o_logo", "o_slogan", "o_uid"].join(",")
                            }
                        },
                        function (err, data) {
                            // console.log(10101010, data);

                            //判断是否有错误信息
                            if (err) {
                                return cb(err, data);
                            }

                            var browseTimes = data.data;

                            if (browseTimes.length > 0) {
                                resultData.browseTimes = browseTimes[0].o_browse_times;
                            }


                            

                            resultData.organizationInfo = browseTimes;
                            cb();
                        }
                    );
                
            },
            /*//4.艺论坛互动、文章数量
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_question + "/count/1/fc_oid/" + restfulParams.organization_id,
                        method: 'POST',
                        data: {
                            fields: ["fc_id"].join(",")
                        }
                    },
                    function (err, data) {
                        // console.log(11111111, data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.artcleCountData = data.count;

                        cb();
                    }
                );

            },*/
            //5.互动用户数量
            /*function (cb) {
                // //console.log(5555);
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganizationConcern + "/count/1/uoc_oid/" + restfulParams.organization_id,
                        method: 'POST',
                        data: {
                            fields: ["uoc_id"].join(",")
                        }
                    },
                    function (err, data) {
                        console.log(12121212,data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.userCountData = data.count;
                        cb();
                    }
                );

            },*/
            function (cb) {

                if(resultData.organizationInfo.length == 0){
                    cb()
                }else{

                    var organizationInfo = resultData.organizationInfo[0];
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.searchContentAnswer + "/fca_uid/" + organizationInfo.o_uid,
                            method: 'POST',
                            data: {
                                fields: ["fca_zan_count", "fca_comment_count"].join(",")
                            }
                        },
                        function (err, data) {
                            //判断是否有错误信息
                            if (err) {
                                return cb(err, data);
                            }

                            var answerData = data.data;
                            var answerCount = data.data.length;
                            if (answerCount > 0) {
                                var zanCount = 0;
                                var commentCount = 0;
                                for (var i = 0; i < answerCount; i++) {
                                    zanCount += parseInt(answerData[i].fca_zan_count);
                                    commentCount += parseInt(answerData[i].fca_comment_count);
                                }
                                var interactionCount = parseInt(zanCount) + parseInt(commentCount);

                                /*console.log("----->");
                                console.log(zanCount);
                                console.log("-----<");
                                console.log(commentCount);
                                console.log("-----");
                                console.log(answerData);
                                console.log("-----");
                                console.log("2--->"+interactionCount);*/
                                resultData.interactionCountData = interactionCount;
                            } else {
                                resultData.interactionCountData = 0;
                                // console.log("*---------*");
                            }


                            cb();
                        }
                    );
                }
            },
            /*//5.回答数量
            function (cb) {
                //console.log(6666);
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.artContentAnswer + "/count/1",
                        method: 'POST',
                        data: {
                            fields: ["fcac_id"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.answerCountData = data.count;
                        cb();
                    }
                );

            },*/
            //查询平台消息总数
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchPlatformNotification + "/pn_type/1/count/1",
                        method: 'POST',
                        data: {
                            fields: ["pn_id"].join(",")
                        }
                    },
                    function (err, data) {
                        // console.log(131131313, data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        platformNotificationCount = data.count;
                        cb();
                    }
                );

            },
            //查询机构查看过平台消息总数
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganizationViewMessageRecord + "/count/1/ovmr_oid/" + restfulParams.organization_id,
                        method: 'POST',
                        data: {
                            fields: ["ovmr_id"].join(",")
                        }
                    },
                    function (err, data) {
                        // console.log(14141414, data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        organizationHasViewCount = data.count;
                        cb();
                    }
                );

            },
            //用户未查看消息总数
            function (cb) {
                resultData.organizationHasNotViewCount = parseInt(platformNotificationCount) - parseInt(organizationHasViewCount);
                cb()
            },


            function (cb) {
                console.log(resultData.organizationInfo.length)
                console.log(resultData.organizationInfo.length)
                if(resultData.organizationInfo.length == 0){
                    cb()
                }else{
                    console.log(2222);
                    var organizationInfo = resultData.organizationInfo[0];
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.artInviteLogContent + "/count/1/fil_invite_type/0/fil_is_read/0/fil_fid/" + organizationInfo.o_uid,
                            method: 'POST',
                            data: {
                                fields: ["fil_id"].join(",")
                            }
                        },
                        function (err, data) {

                            resultData.invitedCount = data.count;

                            cb();
                        }
                    );
                }
            },
            //查询提现次数
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchWithdrawalRecord + "/count/1/wdr_oid/" + restfulParams.organization_id,
                        method: 'POST',
                        data: {
                            fields: ["wdr_id"].join(",")
                        }
                    },
                    function (err, data) {

                        resultData.withdrawCount = data.count;

                        cb();
                    }
                );
            },
            //查询该机构是否绑定银行卡
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganizationFinanceInfo + "/ofi_oid/" + restfulParams.organization_id,
                        method: 'POST',
                        data: {
                            fields: ["ofi_id"].join(",")
                        }
                    },
                    function (err, data) {

                        if (data.data.length > 0) {
                            resultData.is_binding = 1;
                        } else {
                            resultData.is_binding = 0;
                        }


                        cb();
                    }
                );
            },
            //获取为审核的退款数量
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.refund_order_list + "/o_pay_status/5/count/1/of_check_status/0/o_oid/" + restfulParams.organization_id,
                        method: 'POST',
                        data: {
                            fields: ["o_id"].join(",")
                        }
                    },
                    function (err, data) {

                        resultData.refundCount = data.count;

                        cb();
                    }
                );
            },
            //未验证订单总额
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_order + "/o_oid/" + restfulParams.organization_id + "/o_pay_status/1",
                        method: 'POST',
                        data: {
                            fields: ["o_pay_money"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        var all_money = data.data;

                        var money = 0;

                        for (var i = 0; i < all_money.length; i++) {

                            money += parseFloat(all_money[i].o_pay_money);

                        }

                        resultData.hasNotVerifiedMoneyData = parseFloat(money).toFixed(2);
                        cb();
                    }
                );

            },
            //已验证订单总额
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_order + "/o_oid/" + restfulParams.organization_id + "/o_pay_status/3",
                        method: 'POST',
                        data: {
                            fields: ["o_pay_money"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        var all_money = data.data;

                        var money = 0;

                        for (var i = 0; i < all_money.length; i++) {

                            money += parseFloat(all_money[i].o_pay_money);

                        }

                        resultData.hasVerifiedMoneyData = parseFloat(parseFloat(money) + parseFloat(hasCommented)).toFixed(2);

                        hasVerifiedMoneyData = resultData.hasVerifiedMoneyData;
                        cb();
                    }
                );

            },
            //获取可提现的额度
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.withdrawalBalance,
                        method: 'POST',
                        data: {
                            wdr_oid: restfulParams.organization_id
                        }
                    },
                    function (err, data) {

                        //console.log(data);

                        resultData.canBeGettenMoney = data.data.diff;

                        cb();
                    }
                );
            },
            //获取机构下的管理员数量
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_user + "/u_type/1/count/1/u_oid/" + restfulParams.organization_id,
                        method: 'POST',
                        data: {
                            fields: ["u_id"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        resultData.manager = data.count;
                        cb();
                    }
                );

            },
        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

};